﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Board1.Info;
using System.Net;
using System.Collections.ObjectModel;

namespace Board1.GetData
{
    public class DataHot100
    {
        public DataHot100()
        {
        }
        private WebClient downloadURL = new WebClient();
        private string webData = null;
        private int start = 0, des1 = 0, des2 = 0, count = 0;
        
        private ObservableCollection<SongData> _topHot100Song = new ObservableCollection<SongData>();
        public ObservableCollection<SongData> TopHot100Songs
        {
            get { return _topHot100Song; }
        }

        public void getDataHot100(string link)
        {
            //tai du lieu tu trang web
            downloadURL.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCallback);
            downloadURL.DownloadStringAsync(new Uri(link));
        }

            

        private void DownloadStringCallback(object sender, DownloadStringCompletedEventArgs e)
        {
            if (!e.Cancelled && e.Error == null)
            {
                webData = (string)e.Result;
            }
            //bat dau xu li du lieu
            while (start < webData.Length && des1 != -1)
            {
                SongData newItem = new SongData();
                des1 = webData.IndexOf("<article id=", start);
                if (des1 == -1) break;
                des2 = webData.IndexOf("</header>", des1);
                if (webData.Substring(des1, des2 - des1).Contains("song_review"))
                {
                    //lay ten bai hat
                    int songStart = webData.IndexOf("<h1>", des1);
                    int songEnd = webData.IndexOf("</h1>", songStart);
                    newItem.songName = webData.Substring(songStart + 4, songEnd - songStart - 4);

                    //lay thong tin artist
                    int artistStart = webData.IndexOf("<a href", songEnd);
                    artistStart = webData.IndexOf(">", artistStart);
                    int artistEnd = webData.IndexOf("</", artistStart);
                    newItem.artistName = webData.Substring(artistStart + 1, artistEnd - artistStart - 1);

                    //lay thong tin album
                    int albumStart = webData.IndexOf("<br>", artistEnd);
                    int albumEnd = webData.IndexOf("</p>", albumStart);
                    newItem.albumName = webData.Substring(albumStart + 12, albumEnd - albumStart - 22);
                    if (newItem.albumName.Length == 0) newItem.albumName = "Single";

                    //lay image link
                    int imgStart = webData.IndexOf("<img", albumEnd);
                    imgStart = webData.IndexOf("src=", imgStart);
                    int imgEnd = webData.IndexOf("alt", imgStart);
                    newItem.imgSource = webData.Substring(imgStart + 5, imgEnd - imgStart - 7);

                    _topHot100Song.Add(newItem);
                    start = des2 + 1;
                    count++;
                    Console.WriteLine(count);
                    //break;
                }
                else break;
            }
        }
    }
}
